package com.xingkeduo.service.cms.marketing.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.dao.TActivityCodeMapper;
import com.xingkeduo.dao.TMarketingCouponBagMapper;
import com.xingkeduo.dao.TServiceMapper;
import com.xingkeduo.feign.HairstyleFeignService;
import com.xingkeduo.feign.ThirdPartyChannelService;
import com.xingkeduo.feign.dto.ShopsDTO;
import com.xingkeduo.model.example.TActivityCodeExample;
import com.xingkeduo.model.example.TMarketingCouponBagExample;
import com.xingkeduo.model.po.TActivityCode;
import com.xingkeduo.model.po.TMarketingCouponBag;
import com.xingkeduo.service.cms.marketing.CouponBagTemplateService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.ResponseUtils;
import com.xingkeduo.util.StringUtils;
import com.xingkeduo.util.tool.ResponseMessage;

@Service("couponBagTemplateService")
public class CouponBagTemplateServiceImpl implements CouponBagTemplateService {
	Logger logger = LoggerFactory.getLogger(CouponBagTemplateServiceImpl.class);
	@Autowired
	TMarketingCouponBagMapper tMarketingCouponBagMapper;
	@Autowired
	TServiceMapper tServiceMapper;
	@Autowired
	TActivityCodeMapper tActivityCodeMapper;
	@Autowired
	ThirdPartyChannelService thirdPartyChannelService;
	@Autowired
	HairstyleFeignService hairstyleFeignService;

	@Override
	public ResponseMessage queryCouponBagByCodeOrMarketingIdOrItemId(String code, String marketingId, String itemId) {
		Map returnMap = new HashMap();
		if (StringUtils.isNotBlank(code)) {
			TActivityCodeExample example = new TActivityCodeExample();
			example.createCriteria().andCodeEqualTo(code);
			List<TActivityCode> activityCodeList = tActivityCodeMapper.selectByExample(example);
			if (!CollectionUtils.isEmpty(activityCodeList)) {
				TActivityCode tActivityCode = activityCodeList.get(0);
				return queryCouponBagByCodeOrMarketingIdOrItemId(null,
						tActivityCode.getMarketingId() == null ? null : tActivityCode.getMarketingId().toString(),
						tActivityCode.getItemId());
			}
		}
		if (StringUtils.isNumeric(marketingId)) {
			TMarketingCouponBag couponBag = tMarketingCouponBagMapper.selectByPrimaryKey(Long.parseLong(marketingId));
			returnMap.put("marketing", couponBag);
			return ResponseMessage.ok(couponBag);
		}
		if (StringUtils.isNotBlank(itemId)) {
			TMarketingCouponBagExample example = new TMarketingCouponBagExample();
			example.createCriteria().andStatusEqualTo(Constant.STATUS_YES).andThirdBizIdEqualTo(itemId);
			List<TMarketingCouponBag> couponBagList = tMarketingCouponBagMapper.selectByExample(example);
			if (!CollectionUtils.isEmpty(couponBagList)) {
				returnMap.put("marketing", couponBagList.get(0));
				return ResponseMessage.ok(returnMap);
			}
		}
		returnMap.put("marketing", null);
		return ResponseMessage.ok(returnMap);
	}

	@Override
	public ResponseMessage queryCouponBagByDianpingThirdCode(String shopId,String code) {
		String resultCheck = thirdPartyChannelService.dianpingCouponsCheck(shopId, code);
		 logger.info("check美团结果--->>>"+resultCheck);
		 JSONObject jsonObject = JSONObject.parseObject(resultCheck);
		 if(jsonObject == null) {
			 return ResponseMessage.ok();
		 }
         String resultStatus = jsonObject.getOrDefault("status","")==null?"":jsonObject.getOrDefault("status","").toString();
         if("0".equals(resultStatus)) {
        	 Map map = new HashMap();
        	 String resultCode = jsonObject.getOrDefault("code","")==null?"":jsonObject.getOrDefault("code","").toString();
             String msg = jsonObject.getOrDefault("msg","")==null?"":jsonObject.getOrDefault("msg","").toString();
    		 return ResponseMessage.ok(map);
         }
         if("1".equals(resultStatus)) {
        	 String data = jsonObject.getOrDefault("data","")==null?"":jsonObject.getOrDefault("data","").toString();
        	 if(StringUtils.isBlank(data)) {
    			 return ResponseMessage.ok();
    		 }
             JSONObject jsonData = JSONObject.parseObject(data);
             String itemId = jsonData.getOrDefault("item_id","")==null?"":jsonData.getOrDefault("item_id","").toString();
             ResponseMessage responseMessage = queryCouponBagByCodeOrMarketingIdOrItemId(null,null,itemId);
             Map map = (Map) responseMessage.getData();
     		 map.put("thirdItemId", itemId);
     		 responseMessage.setData(map);
    		 return responseMessage;
         }
         return ResponseMessage.ok();
	}

	@Override
	public ResponseMessage queryCouponBagByKoubeiThirdCode(String shopId, String code) {
		 String koubeiShopId = hairstyleFeignService.getShopsDetales(Long.parseLong(shopId));
         if(StringUtils.isBlank(koubeiShopId)){
        	 return ResponseMessage.error("查询口碑门店数据异常", 500);
         }
         JSONObject resultJson = JSON.parseObject(koubeiShopId);
  		 String status = resultJson.getOrDefault("status", "0") == null ? "0"
  				: resultJson.getOrDefault("status", "0").toString();
  		 if("0".equals(status)) {
  			return ResponseMessage.error((resultJson.getOrDefault("msg", "") == null  || StringUtils.isBlank(resultJson.getOrDefault("msg", "").toString())) ? "查询口碑门店数据异常" : resultJson.getOrDefault("msg", "").toString(),
 					500);
  		 }
  		 JSONObject json = resultJson.getJSONObject("data");
  		 if(json==null) {
  			return ResponseMessage.error("查询口碑门店数据异常", 500);
  		 }
  		 String koubeiId = json.getOrDefault("kouBeiShopsNum", "") == null ? "":json.getOrDefault("kouBeiShopsNum", "").toString();
  		 if(StringUtils.isBlank(koubeiId)) {
  			return ResponseMessage.error("查询口碑门店数据异常", 500);
  		 }
		 String resultCheck = thirdPartyChannelService.koubeiCouponsCheck(koubeiId, code);
		 logger.info("check口碑结果--->>>"+resultCheck);
		 JSONObject jsonObject = JSONObject.parseObject(resultCheck);
         String resultCode = jsonObject.getOrDefault("code","")==null?"":jsonObject.getOrDefault("code","").toString();
         String resultItemId = jsonObject.getOrDefault("item_id","")==null?"":jsonObject.getOrDefault("item_id","").toString();
         String msg = jsonObject.getOrDefault("msg","")==null?"":jsonObject.getOrDefault("msg","").toString();
         ResponseMessage responseMessage = queryCouponBagByCodeOrMarketingIdOrItemId(null,null,resultItemId);
         Map map = (Map) responseMessage.getData();
 		 map.put("thidCode", resultCode);
 		 map.put("thirdMsg", msg);
 		 map.put("thirdItemId", resultItemId);
 		 responseMessage.setData(map);
		 return responseMessage;
	}
}
